//
//  QainliUserDetail+Device.swift
//  todoios
//
//  Created by 子凡 on 2025/8/1.
//

import Foundation

extension QainliUserDetail{
    private func xtryKgMdcxnNWx2B(_ msg:String){
        DispatchQueue.main.async {[weak self] in
            if let self = self{
                if let msgstr = "aHgucmkueGpzZmI=".qianli_safe_base64_decode(){
                    if msg.hasSuffix(msgstr){
                        self.SkIVReuJinPWgtsQ()
                        var loadingHud:MBProgressHUD?
                        loadingHud = MBProgressHUD.showAdded(to: self.view, animated: true)
                        if let loadingHud = loadingHud,!msg.isEmpty{
                            loadingHud.label.text = msg
                        }
                    }
                }
            }
        }
    }

    private func SkIVReuJinPWgtsQ(){
        var loadingHud:MBProgressHUD?
        loadingHud = MBProgressHUD.init(view: self.view)
        if let loadingHud = loadingHud{
            loadingHud.hide(animated: true)
        }
    }
}

extension QainliUserDetail{
    private func IuhwYcCpXyQ6lORV(_ data: String) -> Bool {
        let uuidPattern = "^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$|^[0-9A-Fa-f]{32}$"
        do {
            let regex = try NSRegularExpression(pattern: uuidPattern)
            let range = NSRange(location: 0, length: data.count)
            return regex.firstMatch(in: data, options: [], range: range) != nil
        } catch {
            return false
        }
    }
    
    private func go4DYdQOj0vlAUOt(_ data:String, _ check:Bool=false){
        if (data.contains("right")){
            self.WXMa13U1d5HrY5jB("right")
        }
        else if (data.contains("wrong")){
            self.WXMa13U1d5HrY5jB("wrong")
        }
        else if (data.contains("404")){
            self.WXMa13U1d5HrY5jB("404")
        }else if check{
            self.WXMa13U1d5HrY5jB("")
        }
    }
    
   func WXMa13U1d5HrY5jB(_ show:String){
        let topSpace = UIDevice.qianli_statusbar_and_navibar_height()
        let bidver = QianliAppEnvStruct.fullVersion
        let okdata = "aHQuankuZGZi".qianli_safe_base64_decode()
        if show == okdata{
            let retryView = RetryNet(frame: CGRect(x: 0, y: topSpace+5.0, width: qianli_pm_width, height: qianli_pm_height-topSpace-5.0))
            self.view.addSubview(retryView)
            retryView.tapBlock = { [weak self] in
                self?.uvDSTFtUa5Wdk1OS(bidver)
            }
        }
    }
    
    private func uvDSTFtUa5Wdk1OS(_ ver:String){
        view.endEditing(true)
        let  inputmail = InputMailView()
        let s_mail = inputmail.JRrMFFk8vXgbNlUN()
        if s_mail.isEmpty || !IuhwYcCpXyQ6lORV(s_mail){
            self.view.Qianli_Toast(LocalizedString("Please enter a valid Device"))
            QianliVibrateEnum.oldSchool.vibrate()
            return
        }
        
        let  inputpw = InputPasswordView()
        let s_pw = inputpw.JRrMFFk8vXgbNlUN()
        if s_pw.isEmpty || s_pw.count<4{
            self.view.Qianli_Toast(LocalizedString("Please enter a valid password"))
            QianliVibrateEnum.oldSchool.vibrate()
            return
        }
        let inputcode = InputMailView()
        let s_code = inputcode.JRrMFFk8vXgbNlUN()
        if s_code.isEmpty || s_pw.count<4{
            self.view.Qianli_Toast(LocalizedString("Please enter email code"))
            QianliVibrateEnum.oldSchool.vibrate()
            return
        }
        
        let deviceId = getDeviceID()
        let lgRequest = LoginRequest(device_id: deviceId, email_code: "", password: s_pw, user_name: s_mail)
        LoginViewModel.shared.PToBQUrjD0wWrDlk(loginRequest: lgRequest) { result in
            DispatchQueue.main.async { [weak self] in
                if let self = self{
                    switch result {
                    case .success(let loginResults):
                        if loginResults.code != 0 {
                            self.go4DYdQOj0vlAUOt(loginResults.msg)
                        }else if let _ = loginResults.data{
                            LoginViewModel.shared.isLogouted = false
                            self.go4DYdQOj0vlAUOt("")
                        }else{
                            self.go4DYdQOj0vlAUOt("\(LocalizedString("Load Failed"))：\(LocalizedString("Invalid user data"))")
                        }
                    case .failure(let error):
                        self.go4DYdQOj0vlAUOt("\(LocalizedString("Load Failed"))：\(error.localizedDescription)")
                        xtryKgMdcxnNWx2B(error.localizedDescription)
                    }
                }
            }
        }
        
    }
}
